package com.elong.businesstravel.modules.home.adapter;

import java.util.ArrayList;
import java.util.List;

import com.android.support.jhf.iphonetreeview.PinnedHeaderAdapter;
import com.elong.businesstravel.ELongBusinessTravelApplication;
import com.elong.businesstravel.R;
import com.elong.businesstravel.entitys.City;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SectionIndexer;
import android.widget.TextView;

public class HotelCitySelecAdapter extends BaseExpandableListAdapter implements
		PinnedHeaderAdapter {

	private City mCity;
	private City mSelectCity;
	private ELongBusinessTravelApplication mApplication;
	private Context mContext;

	public HotelCitySelecAdapter(Context context, City city, City selectCity) {
		mCity = city;
		mContext = context;
		mApplication = (ELongBusinessTravelApplication) context.getApplicationContext();
		
		mSelectCity = selectCity;
	}

	@Override
	public int getGroupCount() {
		return mCity.code.size() + 1;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (0 == groupPosition) {
			return mCity.hotList.size();
		} else {
			return mCity.cityList.get(groupPosition - 1).size();
		}
	}

	@Override
	public Object getGroup(int groupPosition) {
		if (0 == groupPosition) {
			return "热门城市";
		}
		return mCity.code.get(groupPosition - 1);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		if (0 == groupPosition) {
			return mCity.hotList.get(childPosition);
		}
		return mCity.cityList.get(groupPosition - 1).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return false;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.group_list_text, null);
		}
		TextView groupTextView = (TextView) convertView
				.findViewById(R.id.groupTextView);
		if (0 == groupPosition) {
			groupTextView.setText("#");
		} else {
			groupTextView.setText(mCity.code.get(groupPosition - 1));
		}
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (null == convertView) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.child_list_text, null);
		}
		TextView childTextView = (TextView) convertView
				.findViewById(R.id.childTextView);
		String cityName = null;
		if (0 == groupPosition) {
			cityName = mCity.hotList.get(childPosition).cityName;
			if(null != mSelectCity && mSelectCity.cityName.equals(cityName)){
//				childTextView.setSelected(true);
			}else {
//				childTextView.setSelected(false);
			}
			if(null != mApplication.mLocationCity && mApplication.mLocationCity.cityName.equals(cityName)){
				cityName += "(当前城市)";
			}
//			childTextView.setText(mCity.hotList.get(childPosition).cityName);
			childTextView.setText(cityName);
		} else {
			cityName = mCity.cityList.get(groupPosition-1).get(childPosition).cityName;
			if(null != mSelectCity && mSelectCity.cityName.equals(cityName)){
//				childTextView.setSelected(true);
			}else {
//				childTextView.setSelected(false);
			}
			if(null != mApplication.mLocationCity && mApplication.mLocationCity.cityName.equals(cityName)){
				cityName += "(当前城市)";
			}
//			childTextView.setText(mCity.cityList.get(groupPosition-1).get(childPosition).cityName);
			childTextView.setText(cityName);
		}
		return convertView;
	}


	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public int getPinnedHeaderState(int groupPosition, int childPosition) {
		return ALL_GROUP_EXPAND_NO_CLICK;
	}

	@Override
	public void configurePinnedHeader(View header, int groupPosition,
			int childPosition, int alpha) {
		TextView groupTextView = (TextView)header.findViewById(R.id.groupTextView);
		if(0 == groupPosition){
		groupTextView.setText("#");
		}else {
			groupTextView.setText(mCity.code.get(groupPosition-1));
		}
	}

	@Override
	public int getPositionForSection(int arg0) {
		return arg0;
	}

	@Override
	public Object[] getSections() {
		List<String> indexList = new ArrayList<String>();
		indexList.add("#");
		indexList.addAll(mCity.code);
		return indexList.toArray(new String[indexList.size()]);
	}

	@Override
	public View getPinnedHeadView() {
		return LayoutInflater.from(mContext).inflate(
				R.layout.group_list_text, null);
	}

	@Override
	public void notifyDataSetChanged(ExpandableListView expandableListView) {
		super.notifyDataSetChanged();
		if (PinnedHeaderAdapter.ALL_GROUP_EXPAND_NO_CLICK == getPinnedHeaderState(0, 0)) {
			for (int i = 0; i < getGroupCount(); i++) {
				expandableListView.expandGroup(i);
			}
		}
	}

}
